package org.jeecg.modules.rooms.controller;

import cn.hutool.core.util.EnumUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.Enum.ResultCode;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.rooms.DTO.CesStockTypeDto;
import org.jeecg.modules.rooms.Enum.CouponEnums;
import org.jeecg.modules.rooms.service.CesStockTypeServiceImpl;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品库存分类表 前端控制器
 * </p>
 *
 * @author Claude
 * @since 2023-03-08
 */

@RestController
@Api(tags="商品库存分类")
@Slf4j
@RequestMapping("/rooms/cesStockType")
public class CesStockTypeController {

    @Resource
    private CesStockTypeServiceImpl stockTypeService;


    @ApiOperation(value="分类查询", notes="分类查询")
    @GetMapping(value = "/tree")
    public Result tree(@RequestParam String hotelId){
        if(StringUtils.isBlank(hotelId)){
            return Result.error(ResultCode.PARAM_MISS);
        }
        try{
            return stockTypeService.tree(hotelId);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @ApiOperation(value="顶级分类查询", notes="分类查询")
    @GetMapping(value = "/getTopTypes")
    public Result getTopTypes(@RequestParam String hotelId){
        if(StringUtils.isBlank(hotelId)){
            return Result.error(ResultCode.PARAM_MISS);
        }
        try{
            return stockTypeService.getTopTypes(hotelId);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }
    @ApiOperation(value="下级分类查询", notes="下级分类查询")
    @GetMapping(value = "/getChildTypes")
    public Result getChildTypes(@RequestParam String hotelId,@RequestParam String parentId){
        if(StringUtils.isBlank(hotelId)){
            return Result.error(ResultCode.PARAM_MISS);
        }
        try{
            return stockTypeService.getChildTypes(hotelId,parentId);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    /**
     * 根据posType获取对应分类
     * @param hotelId
     * @param posType
     * @return
     */
    @ApiOperation(value="顶级分类查询", notes="分类查询")
    @GetMapping(value = "/getTopTypesByPosType")
    public Result getTopTypes(@RequestParam String hotelId,@RequestParam String posType){
        if(StringUtils.isBlank(hotelId)){
            return Result.error(ResultCode.PARAM_MISS);
        }
        try{
            return stockTypeService.getTopTypes(hotelId,posType);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }



    @ApiOperation(value="分类创建", notes="分类创建")
    @PostMapping(value = "/create")
    public Result create(@RequestBody CesStockTypeDto dto){
        if(StringUtils.isBlank(dto.getHotelId())){
            return Result.error(ResultCode.PARAM_MISS);
        }
        if(StringUtils.isBlank(dto.getName())){
            return Result.error(ResultCode.PARAM_MISS);
        }
        if(StringUtils.isBlank(dto.getParentId())){
            return Result.error(ResultCode.PARAM_MISS);
        }
        if(StringUtils.isBlank(dto.getApplyScope())){
            return Result.error(ResultCode.PARAM_MISS);
        }
        List<Integer> applyScope = Arrays.stream(dto.getApplyScope().split(",")).map(v -> Integer.parseInt(v)).collect(Collectors.toList());
        List<Object> codes = EnumUtil.getFieldValues(CouponEnums.CesStockTypeApplyScopeEnum.class, "code");
        for (int i = 0; i < applyScope.size(); i++) {
            Integer v = applyScope.get(i);
            if (!codes.contains(v.intValue())) return Result.error("参数不正确!请选择正确的应用范围!");
        }
        if(applyScope.contains(CouponEnums.CesStockTypeApplyScopeEnum.POS.code())){
            //验证
            if(StringUtils.isBlank(dto.getPosType())){
                return Result.error(ResultCode.PARAM_MISS,"POS 类型数据不能为空!");
            }
        }
        if(applyScope.contains(CouponEnums.CesStockTypeApplyScopeEnum.FUN.code())){
            //验证
            if(StringUtils.isBlank(dto.getFunType())){
                return Result.error(ResultCode.PARAM_MISS,"娱乐 类型数据不能为空!");
            }
        }
        if(null == dto.getSort()){
            return Result.error(ResultCode.PARAM_MISS,"排序字段必填");
        }
        try{
            return stockTypeService.create(dto);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }


    @ApiOperation(value="分类修改", notes="分类修改")
    @PutMapping(value = "/modify")
    public Result modify(@RequestBody CesStockTypeDto dto){
        if(StringUtils.isBlank(dto.getId())){
            return Result.error(ResultCode.PARAM_MISS);
        }
        if(StringUtils.isBlank(dto.getHotelId())){
            return Result.error(ResultCode.PARAM_MISS);
        }
        if(StringUtils.isBlank(dto.getName())){
            return Result.error(ResultCode.PARAM_MISS);
        }
        if(StringUtils.isBlank(dto.getParentId())){
            return Result.error(ResultCode.PARAM_MISS);
        }
        if(StringUtils.isBlank(dto.getApplyScope())){
            return Result.error(ResultCode.PARAM_MISS);
        }
        List<Integer> applyScope = Arrays.stream(dto.getApplyScope().split(",")).map(v -> Integer.parseInt(v)).collect(Collectors.toList());
        List<Object> codes = EnumUtil.getFieldValues(CouponEnums.CesStockTypeApplyScopeEnum.class, "code");
        for (int i = 0; i < applyScope.size(); i++) {
            Integer v = applyScope.get(i);
            if (!codes.contains(v.intValue())) return Result.error("参数不正确!请选择正确的应用范围!");
        }
        if(applyScope.contains(CouponEnums.CesStockTypeApplyScopeEnum.POS.code())){
            //验证
            if(StringUtils.isBlank(dto.getPosType())){
                return Result.error(ResultCode.PARAM_MISS,"POS 类型数据不能为空!");
            }
        }
        if(applyScope.contains(CouponEnums.CesStockTypeApplyScopeEnum.FUN.code())){
            //验证
            if(StringUtils.isBlank(dto.getFunType())){
                return Result.error(ResultCode.PARAM_MISS,"娱乐 类型数据不能为空!");
            }
        }
        if(null == dto.getSort()){
            return Result.error(ResultCode.PARAM_MISS,"排序字段必填");
        }
        try{
            return stockTypeService.modify(dto);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }


    @ApiOperation(value="分类删除", notes="分类删除")
    @DeleteMapping(value = "/delete")
    public Result delete(CesStockTypeDto dto) {
        if(StringUtils.isBlank(dto.getId())){
            return Result.error(ResultCode.PARAM_MISS);
        }
        try{
            return stockTypeService.delete(dto);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }
}

